package com.wxmit.framework.excel.handler;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;

import java.util.Objects;

/**
 * @author huangxiong
 * @date 2022/12/23
 */
public class SexConverterContext implements Converter<String> {

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<String> context) throws Exception {
        String value = context.getValue();
        System.out.println("源数据：" + value);
        WriteCellData<String> cellData = new WriteCellData<>(doConvert(value,true));
        return cellData;
    }

    @Override
    public String convertToJavaData(ReadConverterContext<?> context) throws Exception {
        ReadCellData<?> readCellData = context.getReadCellData();
        String value = readCellData.getStringValue();
        return doConvert(value,false) ;
    }

    private String doConvert(String value){
        return doConvert(value,false);
    }

    /**
     *  处理转换
     * @param value 需转换值
     * @param reversal 是否反转（true: code 2 label ，false：label 2 code ）
     * @return 转换值
     */
    private String doConvert(String value,Boolean reversal){
        if(reversal){
            return Objects.equals(value,"0")?"男":"女";
        }else{
            return Objects.equals(value,"男")? "0" : "1" ;
        }
    }
}
